const KEY_NAME = "saved_password";

function encrypt_data(content) {
    let key = sessionStorage.getItem(KEY_NAME);
    console.log("encrypt data: " + content);
    return encrypt_key(content, key);
}

function encrypt_key(content, key = "") {
    let result = CryptoJS.AES.encrypt(content, CryptoJS.enc.Utf8.parse(key), {
        padding: CryptoJS.pad.Pkcs7,
        mode: CryptoJS.mode.ECB
    });
    return result;
}

function decrypt_data(content) {
    let key = sessionStorage.getItem(KEY_NAME);
    console.log("decrypt data: " + content);
    return decrypt_key(content, key);
}

function decrypt_key(content, key = "") {
    var result = CryptoJS.AES.decrypt(content, CryptoJS.enc.Utf8.parse(key), {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    return result.toString(CryptoJS.enc.Utf8);
}

function isLogin() {
    let pwd = sessionStorage.getItem(KEY_NAME);
    return pwd != null;
}


function set_pwd(key) {
    sessionStorage.setItem(KEY_NAME, key);
}

function del_pwd() {
    if (sessionStorage.hasOwnProperty(KEY_NAME))
        sessionStorage.removeItem(KEY_NAME);
}